/***********
 **	框架页js，下面的所有方法均为单例模式，一个页面只能实现一个实例！！！
 ********** */
var homePageUrl = null;
/**系统时间**/
FrameTime = {
	setSysTime: function (id) {
		var time = mini.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
		$("#" + id).html(time);
	},
	// 初始化系统时间，定时更新
	initial: function (id) {
		var that = this;
		that.setSysTime(id);
		setInterval(function () {
			that.setSysTime(id);
		}, 1000);
	}
}

/**菜单（只支持2级菜单）**/
FrameMenu = {
	arrow1_right: ctx_menuIcon + "/dashboard/menuLv1_right.png",
	arrow1_down: ctx_menuIcon + "/dashboard/menuLv1_right.png",
	arrow2_right: ctx_menuIcon + "/dashboard/menuLv2_right.png",
	lastCheckedMenu: null, //缓存上一个点击的一级菜单
	lastCheckedMenu2: null, //缓存上一个点击的二级菜单
	isMenuHide: false, //当前菜单隐藏或显示状态，默认显示
	//初始化菜单
	initial: function (id, data, imgFolder) {
		//从后台获取菜单数据
		var that = this;
		/*		data = [{
					text: "test",
					info: {
						iconCls: 'menu0.png,menu0-hover.png',
						url: "test"
					}
				}, {
					text: "test2",
					info: {
						iconCls: '',
						url: "test2"
					},
					children: [{
						text: "testw",
						info: {
							iconCls: '',
							url: "http://www.baidu.com"
						}
					}]
				}];*/

		//创建菜单
		that.createMenu("menu", data, imgFolder);
		//加载一级菜单点击事件
		that.menuLv1click();
		//加载二级菜单点击事件
		that.menuLv2click();
	},
	//生成菜单dom
	createMenu: function (id, data, imgFolder) {
		var menu_html = "";
		if (data) {
			//debugger
			for (var i = 0; i < data.length; i++) {
				var menuItem = data[i]; //一级菜单
				var menuImg = menuItem.img; //一级菜单图标
				var menuImgHover = menuItem.imgHover; //一级菜单图标(选中状态)
				if (menuImg) {
					menuImg = imgFolder + menuImg;
				} else {
					menuImg = ctx_menuIcon + "/dashboard/menu-icon/menu3.png";
				}
				if (menuImgHover) {
					menuImgHover = imgFolder + menuImgHover;
				} else {
					menuImgHover = ctx_menuIcon + "/dashboard/menu-icon/menu3-hover.png";
				}
				var title = menuItem.label;
				var url = ctx + menuItem.path;
				var children = menuItem.children; //二级菜单
				if (title == "首页" && url != null && url != "") {
					homePageUrl = url;
				}
				menu_html += "<li>";
				menu_html += "	<div class='frame-menu-Lv1' isChecked='false' name='" + title + "' url='" + url + "'>";
				menu_html += "<img src='" + menuImg + "' title='" + title + "' class='frame-menu-icon' imgsrc='" + menuImg + "' imgsrc-hover='" + menuImgHover + "'>";
				menu_html += "<p>" + title + "</p>";
				menu_html += "<img src='" + this.arrow1_right + "' class='frame-menu-arrow'>";
				menu_html += "	</div>";
				if (children && children.length > 0) {
					menu_html += "<ul style='display: none' class='frame-menu-Lv2'>";
					for (var j = 0; j < children.length; j++) {
						var menuItem2 = children[j]; //二级菜单
						var title2 = menuItem2.label;
						var url2 = ctx + menuItem2.path;
						if (title2 == "首页" && homePageUrl != null && url != null && url != "") {
							homePageUrl = url2;
						}
						menu_html += "<li class='frame-menu-Lv2-item' name='" + title2 + "' url='" + url2 + "'>";
						menu_html += "<img src='" + this.arrow2_right + "'>";
						menu_html += title2;
						menu_html += "</li>";
					}
					menu_html += "</ul>";
				}
				menu_html += "</li>";
			}
		}
		$("#" + id).html(menu_html);
	},
	//一级菜单点击事件
	menuLv1click: function () {
		var that = this;
		$(".frame-menu-Lv1").click(function () {
			if (that.isMenuHide) {
				that.hideMenu();
			}
			//取消上次点击的菜单(多次点击自己不触发此事件)
			if (that.lastCheckedMenu != null && that.lastCheckedMenu != this) {
				that.cancelCheckedMenu(that.lastCheckedMenu, false); //上次点击控件,是否需要收起二级菜单
			}
			//缓存本次点击的菜单
			that.lastCheckedMenu = this;
			//菜单点击效果切换
			var isChecked = $(this).attr('isChecked'); // 当前选中状态
			var menuLv2 = $(this).parent().find("ul");
			if (menuLv2.length == 0) { //当前没有子菜单时点击永远是选中状态，不切换
				that.checkedMenu(this, null); //当前控件,二级子菜单
			} else {
				if (isChecked == 'false') {
					that.checkedMenu(this, menuLv2); //当前控件,二级子菜单
				} else {
					that.cancelCheckedMenu(this, true); //当前控件,是否需要收起二级菜单
				}
			}

		});
	},
	//一级菜单菜单点击撑开选中事件
	checkedMenu: function (e, menuLv2) {
		//1.显示二级菜单；2.切换一级菜单图标为选中状态；3-切换一级菜单右侧下拉箭头为展开状态
		$(e).attr('isChecked', 'true');
		$(e).find(".frame-menu-icon").attr('src', $(e).find(".frame-menu-icon").attr('imgsrc-hover'));
		$(e).find(".frame-menu-arrow").attr('src', this.arrow1_down);
		$(e).addClass("frame-menu-Lv1-active");
		//二级菜单
		if (menuLv2 != null) {
			menuLv2.show();
		} else {
			//没有二级菜单时直接打开页面
			FrameTab.addTab($(e).attr("name"), $(e).attr("url"));
		}
	},
	//一级菜单点击收起取消事件
	cancelCheckedMenu: function (e, isClose) {
		//1.隐藏二级菜单；2.切换一级菜单图标为取消选中状态；3-切换一级菜单右侧下拉箭头为收起状态
		//原菜单再次点击时取消原菜单点击状态；点击其他菜单时，原菜单点击状态保留，以便下次点击时取消点击状态！
		if (isClose) {
			$(e).attr('isChecked', 'false');
		}
		$(e).find(".frame-menu-icon").attr('src', $(e).find(".frame-menu-icon").attr('imgsrc'));
		$(e).find(".frame-menu-arrow").attr('src', this.arrow1_right);
		$(e).removeClass("frame-menu-Lv1-active");
		//原菜单再次点击时收起二级菜单；点击其他菜单时，原菜单二级菜单不收起！
		if (isClose) {
			var menuLv2 = $(e).parent().find("ul");
			if (menuLv2.length > 0) {
				menuLv2.hide();
			}
		}
	},
	//二级菜单点击事件
	menuLv2click: function () {
		var that = this;
		$(".frame-menu-Lv2-item").click(function () {
			//取消上次点击的菜单(多次点击自己不触发此事件)
			if (that.lastCheckedMenu2 != null && that.lastCheckedMenu2 != this) {
				$(that.lastCheckedMenu2).removeClass("frame-menu-Lv2-item-active");
			}
			that.lastCheckedMenu2 = this;
			//本次点击选中状态样式
			$(this).addClass("frame-menu-Lv2-item-active");
			//打开页面
			FrameTab.addTab($(this).attr("name"), $(this).attr("url"));
		});
	},
	//收起菜单
	hideMenu: function () {
		if (this.isMenuHide == false) {
			this.isMenuHide = true;
			/*$(".frame-menu-Lv1").attr("isChecked",false);*/
			$(".frame-menu-Lv2").hide();
			$(".frame-menu-icon").addClass("frame-menu-icon-min");
			$(".frame-left").addClass("frame-left-min");
			$(".frame-center").animate({
				left: '60px',
				width: '+=152px'
			}, function () {
				setTimeout(function () {
					FrameTab.doLayout(); //mini-tab重新布局
				}, 500);
			});
		} else {
			this.isMenuHide = false;
			$(".frame-menu-icon").removeClass("frame-menu-icon-min");
			$(".frame-left").removeClass("frame-left-min");
			$(".frame-center").animate({
				left: '212px',
				width: '-=152px'
			}, function () {
				setTimeout(function () {
					FrameTab.doLayout(); //mini-tab重新布局
				}, 500);
			});
			//打开所有之前展开的二级菜单
			$(".frame-menu-Lv1").each(function (i, e) {
				var isChecked = $(e).attr("isChecked");
				console.log(isChecked);
				if (isChecked == 'true') {
					$(e).parent().find(".frame-menu-Lv2").show();
				}
			});
		}

	}
}

/**tab页签**/
FrameTab = {
	tabs: null,
	currentTab: null,
	//初始化
	initial: function (id) {
		this.tabs = mini.get(id);
	},
	//tab右键菜单onBeforeOpen
	onBeforeOpen: function (e) {
		FrameTab.currentTab = FrameTab.tabs.getTabByEvent(e.htmlEvent);
		if (!FrameTab.currentTab) {
			e.cancel = true;
		}
	},
	//tab右键菜单-关闭其他
	closeAllBut: function () {
		var but = [FrameTab.currentTab];
		but.push(FrameTab.tabs.getTab("首页"));
		FrameTab.tabs.removeAll(but);
	},
	//tab右键菜单-关闭所有
	closeAll: function () {
		var but = [];
		but.push(FrameTab.tabs.getTab("首页"));
		FrameTab.tabs.removeAll(but);
	},
	//tab右键菜单-关闭当前
	closeTab: function () {
		if (FrameTab.currentTab.name == "首页") {
			showFailTip("首页无法关闭");
		} else {
			FrameTab.tabs.removeTab(FrameTab.currentTab);
		}
	},
	//增加tab页
	addTab: function (name, url) {
		if (url == "" || url == null) {
			return;
		}
		// debugger
		var tab = this.tabs.getTab(name);
		if (tab == undefined) {
			tab = {
				title: name,
				name: name
			}
			//这里拼接了url，实际项目，应该从后台直接获得完整的url地址
			if (url.indexOf("http://") != -1 || url.indexOf("https://") != -1) {
				tab.url = url;
			} else if (url.indexOf("\$\{") != -1 && url.indexOf("\}") != -1) {
				var key = url.substring(url.indexOf("\$\{") + 2, url.indexOf("\}"));
				var val = getProperties(key);
				tab.url = url.replace("\$\{" + key + "\}", val);
			} else {
				tab.url = ctx + url;
			}
			if (name == "首页") {
				tab.showCloseButton = false;
			} else {
				tab.showCloseButton = true;
			}
			this.tabs.addTab(tab);
			this.tabs.activeTab(tab);
		} else {
			this.tabs.activeTab(tab);
			//不重载页面，防止被保存数据丢失?
			this.tabs.reloadTab(tab);
		}
		return tab;
	},
	//首页显示
	showHomePage: function (url) {
		if (homePageUrl != null) {
			this.addTab('首页', homePageUrl);
		} else {
			this.addTab('首页', url);
		}
	},
	doLayout: function () {
		this.tabs.doLayout();
	}

}

function getProperties(key) {
	var result;
	$.ajax({
		url: ctx + "/sys/getProperties.do",
		data: {
			key: key
		},
		async: false,
		success: function (data) {
			result = data;
		}
	});
	return result;
}

/**全屏**/
FullScreen = {
	open: function () {
		var docElm = document.documentElement;
		if (docElm.requestFullscreen) { //W3C
			docElm.requestFullscreen();
		} else if (docElm.mozRequestFullScreen) { //FireFox
			docElm.mozRequestFullScreen();
		} else if (docElm.webkitRequestFullScreen) { //Chrome等
			docElm.webkitRequestFullScreen();
		} else if (elem.msRequestFullscreen) { //IE11
			elem.msRequestFullscreen();
		}
		$("#fullScreen_open").hide();
		$("#fullScreen_closed").show();
	},
	closed: function () {
		if (document.exitFullscreen) {
			document.exitFullscreen();
		} else if (document.mozCancelFullScreen) {
			document.mozCancelFullScreen();
		} else if (document.webkitCancelFullScreen) {
			document.webkitCancelFullScreen();
		} else if (document.msExitFullscreen) {
			document.msExitFullscreen();
		}
		$("#fullScreen_open").show();
		$("#fullScreen_closed").hide();
	}
}